const { search, insert, update, del, findOne } = require('../model/type.model');

class TypeService {
  // 查询类型列表
  async searchType({ pageNum, pageSize }) {
    const page = (pageNum-1) * pageSize;

    // 分页
    let whereLimit = `limit ${page}, ${pageSize}`;
    
    const sql = `select * from b_type ${whereLimit}`;
    const res = await search(sql)
    return res
  }

   // 查询标类型表总数
   async searchTypeTotal() {
   
    const sql = `select count(*) as total from b_type`;
    const res = await search(sql)
    return res
  }

  // 添加类型
  async insertType({type_name}) {
    const params = { type_name }
    const res = await insert('b_type', params)
    return res
  }
  // 修改类型
  async updateType({id, type_name}) {
    const params = {}
    const whereOpt = {}

    id && Object.assign(whereOpt, { id })
    type_name && Object.assign(params, { type_name })
  
    const res = await update('b_type', params, whereOpt)
    return res
  }
  // 删除类型
  async deleteType({id}) {
    const params = {}

    id && Object.assign(params, { id })
    const res = await del('b_type', params)
    return res
  }

  // 查询某一个类型
  async findType({id, type_name}) {
    const params = {}

    id && Object.assign(params, { id })
    type_name && Object.assign(params, { type_name })

    const res = await findOne('b_type', params)
    return res.length > 0 ? res : null
  }
}

module.exports = new TypeService();
